﻿using System.Collections.Generic;
using System.Windows.Forms;

namespace Borg
{
    public class DataGridViewSupport
    {
        public static void SelectColumns(DataGridView dataGridView, IEnumerable<IAttributeDescriptor> attrList)
        {
            if (dataGridView == null)
                return;

            var i = 0;

            foreach (DataGridViewBand dataGridViewBand in dataGridView.Columns)
                dataGridViewBand.Visible = false;

            foreach (var attributeDescriptor in attrList)
            {
                var dataGridViewColumn = dataGridView.Columns[attributeDescriptor.FieldName];

                if (dataGridViewColumn == null)
                    continue;

                dataGridViewColumn.DisplayIndex = i++;
                dataGridViewColumn.HeaderText = attributeDescriptor.ListCaption;

                if (attributeDescriptor.Width != -1)
                    dataGridViewColumn.Width = attributeDescriptor.Width;

                dataGridViewColumn.Visible = true;
            }
        }

        public static void SetAttributes(DataGridView dataGridView, IAttributeProvider attributes)
        {
            if (attributes != null)
                SelectColumns(dataGridView, attributes.SelectedAttributes);
            else
                foreach (DataGridViewColumn column in dataGridView.Columns)
                {
                    column.Visible = true;
                    column.HeaderText = column.Name;
                }
        }
    }
}

